package com.forum.service.Impl;

import com.forum.dao.UserDaoMapper;
import com.forum.pojo.User;
import com.forum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDaoMapper userDaoMapper;

    @Override
    public int deleteUser(String userID) {
        return userDaoMapper.deleteUser(userID);
    }

    public void setUserDaoMapper(UserDaoMapper userDaoMapper) {
        this.userDaoMapper = userDaoMapper;
    }

    @Override
    public int addUser(User user) {
        return userDaoMapper.save(user);
    }

    @Override
    public int updateUser(User user) {
        return userDaoMapper.updateUser(user);
    }

    @Override
    public User findUserById(String userID) {
        User u =new User();
        u.setUserID(userID);
        List<User> listu = userDaoMapper.findUser(u);
        if(listu.size()<1) return null;
        return listu.get(0);
    }

    @Override
    public User findUserByUsername(String username) {
        User u =new User();
        u.setUsername(username);
        List<User> listu = userDaoMapper.findUser(u);
        if(listu.size()<1) return null;
        return listu.get(0);
    }

    @Override
    public List<User> findAllUser() {
        User u =new User();
        List<User> listu = userDaoMapper.findUser(u);
        return listu;
    }

    @Override
    public User UserLogin(User user) {
        List<User> listu = userDaoMapper.findUser(user);
        if(listu.size()<1) return null;
        return listu.get(0);
    }
}
